﻿//Copyright 2011 University of Southampton 
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using EntityEditorLib;
using System.Diagnostics;
using Exceptions;
using SharePointEntityProject.Contracts;
using System.Xml.Serialization;

namespace SharePointEntityProject.VisualElementCreator
{
    [ToolboxItemAttribute(false)]
    public partial class VisualElementCreator : WebPart //, IEntityEditorContract
    {
        private string _EntityEditorListName;
        [WebBrowsable(true), Category("Entity Editor Settings"), Personalizable, Description("Name of the storage list")]
        public string EntityEditorListName
        {
            get
            {
                 
                return _EntityEditorListName;
            }
            set
            {
                _EntityEditorListName = value;
            }
        }


       // [WebBrowsable(true)]
       // [Personalizable()]
     //   public string ListDescription { get; set; }

        private string _SiteURL;
        [WebBrowsable(true), Category("Entity Editor Settings"), Personalizable, Description("Set the site URL of the site")]
        public string SiteURL
        {
            get
            {
                return _SiteURL;
            }
            set
            {
                _SiteURL = value;
            }
        }

        //Allow public viewing of listname between webparts
      //  [ConnectionProvider("Storage info provider")]
     //   public IEntityEditorContract GetListNameCommunicationPoint()
     //   {
     //       return this as IEntityEditorContract;
      //  }

        private EntityEditor entityEdit;
        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);

            InitializeControl();
            Debug.WriteLine("Started");
            entityEdit = new EntityEditor(this.SiteURL, this.EntityEditorListName);
            if (String.IsNullOrEmpty(SiteURL))
            {
                this.SiteURL = SPContext.Current.Site.Url;
                this.StatusTxtBox.Text = "Configure the webpart";

            }
            else
            {
                this.StatusTxtBox.Text = string.Empty;


                //Ensure the list for this web part exists. (EVERY TIME)

                try
                {
                    string ListName = this.EntityEditorListName;
                    string ListDescription = Properties.Resources.DewfaultListDescription;
                    entityEdit.CreateEntityStorageList(ListName, ListDescription);
                }
                catch (Exception ex)
                {

                    this.StatusTxtBox.Text = ex.Message;
                }
            }

 
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {




        }

        protected void CreateEntity_Click(object sender, EventArgs e)
        {
            string entityName;

            //TODO: Validate the txt boxes 
            try
            {
                //Create entity
                  entityName = EntityNameTxtBox.Text;//TODO:Validate
                string entityDesc = EntityDescriptionTxtBox.Text;//TODO:Validate
                SPContentType newEntity = entityEdit.CreateNewEntity(this.EntityEditorListName, entityName, entityDesc);

                //reset the boxes
                EntityNameTxtBox.Text = string.Empty;
                EntityDescriptionTxtBox.Text = string.Empty;

            }
            catch (Exception ex)
            {

                StatusTxtBox.Text = ex.Message;//TODO: Format this text better
                return;
            }

            //if success do some user feedback
            StatusTxtBox.Text = String.Format("{0} Entity created", entityName);
        }



    }
}
